package team.you.change.service.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.you.change.dao.ClassMapper;
import team.you.change.pojo.Class;
import team.you.change.service.ClassService;
import team.you.change.util.Result;
import team.you.change.util.YuoUtil;

import java.util.*;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    private ClassMapper classMapper;
    @Override
    public Result getAllClasses(Integer page, Integer limit) {
        Result result = new Result();
        List<Class> classes = classMapper.selectAll(YuoUtil.getPage(page, limit), limit);
        Integer count = classMapper.getCount();
        return YuoUtil.setData(result, classes, count);
    }

    @Override
    public Result del(String id) {
        Result result = new Result();
        Class aClass = classMapper.selectByPrimaryKey(id);
        try {
            if (aClass != null) {
                return YuoUtil.crudMsg(classMapper.deleteByPrimaryKey(id), result);
            } else {
                result.setCode(500);
                result.setMsg("该行不存在");
            }
            return result;
        } catch (Exception e) {
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    @Transactional
    public Result delete(String[] ids) {
        Result result = new Result();
        try {
            for (int i = 0; i < ids.length; i++) {
                classMapper.deleteByPrimaryKey(ids[i]);
            }
            return result;
        } catch (Exception e) {
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    public Result update(Class clas) {
        Result result = new Result();
        try {
            return YuoUtil.crudMsg(classMapper.updateByPrimaryKey(clas), result);
        } catch (Exception e) {
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    public Result add(Class clas) {
        Result result = new Result();
        String uuid = UUID.randomUUID().toString();
        if(clas != null){
            clas.setId(uuid);
        }
        try {
            return YuoUtil.crudMsg(classMapper.insert(clas), result);
        }catch (Exception e){
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    public Result selectByLike(String value, Integer college, Integer major, Integer page, Integer limit) {
        Result result = new Result();
        List<Class> classes = classMapper.selectByLike(value, college, major, YuoUtil.getPage(page, limit), limit); //模糊查询时分页
        List<Class> count = classMapper.selectByLike(value, college, major, null, null);
        return YuoUtil.setData(result, classes, count.size());
    }

    @Override
    public Result selCode(String code) {
        Result result = new Result();
        Class aClass = classMapper.selectCode(code);
        if(aClass == null){
            return result;
        }else {
            result.setCode(500);
            result.setMsg("班级代码重复");
            return result;
        }
    }

    @Override
    public Result getClassOption() {
        Result result = new Result();
        //限制班级年份
        Calendar cal = Calendar.getInstance(); //获取当前时间
        int i = cal.get(Calendar.YEAR); //取年份
        int month = cal.get(Calendar.MONTH) + 1;
        int year = i % 100; // 年份后2位
        if (month > 6) year++; //月份大于6，第二年
        List<Class> classes = classMapper.selectAllId();
        //根据年份处理数据
        Iterator<Class> iterator = classes.iterator();
        while (iterator.hasNext()){
            Class next = iterator.next();
            Integer classYear = Integer.parseInt(next.getClassCode().substring(1, 3)); //获取班级年份，转为整型
            if (classYear + 4 < year){ //班级以毕业
                iterator.remove();
            }
        }
        result.setData(classes);
        return result;
    }

}
